package com.company;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 *
 */

public class ReminderApp {

    private JFrame mainFrame;
    private JTextField timeField;
    private JTextField messageField;
    private JButton setReminderButton;
    private List<Reminder> reminders = new ArrayList<>();
    private Timer timer;

    public ReminderApp() {
        prepareGUI();
    }

    private void prepareGUI() {
        //  设置主窗口大小，排版类型 退出终止窗口
        mainFrame = new JFrame("Reminder App");
        mainFrame.setSize(400, 200);
        mainFrame.setLayout(new FlowLayout());
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 设置提醒1时间时间输入
        JLabel timeLabel = new JLabel("Enter Time (HH:mm):");
        timeField = new JTextField(20);
        timeLabel.setLabelFor(timeField);
        mainFrame.add(timeLabel);
        mainFrame.add(timeField);

        // 设置提醒内容
        JLabel messageLabel = new JLabel("Enter Reminder Message:");
        messageField = new JTextField("提醒的时间到了！",30);
        messageLabel.setLabelFor(messageField);
        mainFrame.add(messageLabel);
        mainFrame.add(messageField);

        // 添加点击事件
        setReminderButton = new JButton("Set Reminder");
        setReminderButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setReminder();
            }
        });
        mainFrame.add(setReminderButton);

        mainFrame.setVisible(true);
    }

    private void setReminder() {
        String timeStr = timeField.getText();
        String message = messageField.getText();

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        try {
            Date time = sdf.parse(timeStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.set(Calendar.HOUR_OF_DAY, time.getHours());
            calendar.set(Calendar.MINUTE, time.getMinutes());
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);

            if (calendar.before(Calendar.getInstance())) {
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }

            Reminder reminder = new Reminder(calendar.getTime(), message);
            reminders.add(reminder);
            scheduleReminder(reminder);

            JOptionPane.showMessageDialog(mainFrame, "Reminder set successfully!");
            timeField.setText("");
            messageField.setText("");
        } catch (ParseException ex) {
            JOptionPane.showMessageDialog(mainFrame, "Invalid time format! Please use HH:mm");
        }
    }

    private void scheduleReminder(Reminder reminder) {
        if (timer == null) {
            timer = new Timer();
        }

        timer.schedule(new ReminderTask(reminder), reminder.getTime().getTime() - System.currentTimeMillis());
    }

    private class Reminder {
        private Date time;
        private String message;

        public Reminder(Date time, String message) {
            this.time = time;
            this.message = message;
        }

        public Date getTime() {
            return time;
        }

        public String getMessage() {
            return message;
        }
    }

    private class ReminderTask extends TimerTask {
        private Reminder reminder;

        public ReminderTask(Reminder reminder) {
            this.reminder = reminder;
        }

        @Override
        public void run() {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    // 创建并显示提醒窗口
                    ReminderWindow window = new ReminderWindow(reminder.getMessage());
                    window.setVisible(true); // 显示窗口

                    // 如果需要窗口自动关闭，可以在这里添加逻辑，例如使用定时器或者监听用户的交互来关闭窗口
                    // 例如，在ReminderWindow类中添加一个方法用来关闭窗口，然后在这里调用它
                    // window.closeAfterSomeTime(); // 假设存在这样一个方法

                    // 如果窗口已经关闭，并且不需要再次触发这个提醒，可以从提醒列表中移除它
                    // reminders.remove(reminder); // 假设这是你想要的行为
                }
            });
        }
    }
}